//
//  ViewController.swift
//  calculator
//
//  Created by Juvid on 2016/12/9.
//  Copyright © 2016年 Juvid. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var juTxtResult: UITextView!
    @IBOutlet weak var juLabNum: UILabel!
    var numFirst = 0.0
    var numOperation = 0
    var startOper = false
    var arrResult = NSMutableArray.init()


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.
    }

    /// 数字
    @IBAction func juTouchNum(_ sender: UIButton) {
        if startOper {
            juLabNum.text=sender.currentTitle!
            startOper = false
            return
        }
        if juLabNum.text == "0" {
            juLabNum.text=sender.currentTitle!
        }else{
            juLabNum.text = juLabNum.text! + sender.currentTitle! ;
        }
    }

    /// 小数点
    @IBAction func juTouchPoint(_ sender: UIButton) {
        if startOper {
            startOper = false
            juLabNum.text = "0" + sender.currentTitle!
            return
        }
        let  rang = juLabNum.text?.range(of: ".")
        if (rang == nil)  {
            juLabNum.text = juLabNum.text! + sender.currentTitle! ;
        }
    }

    /// 正负号
    @IBAction func juTouchSign(_ sender: Any) {
        let numSum = -(juLabNum.text! as NSString ).doubleValue
        juLabNum.text=self.juDealWith(number: numSum)

    }

    @IBAction func juTouchCollagen(_ sender: Any) {
         let numSum = (juLabNum.text! as NSString ).doubleValue/100.0
         juLabNum.text=self.juDealWith(number: numSum)
    }
    @IBAction func shTouchOperation(_ sender: UIButton) {
        numOperation = sender.tag
        numFirst=(juLabNum.text! as NSString ).doubleValue
        startOper = true

    }
    /// 归零
    @IBAction func juTouchClear(_ sender: UIButton) {
        juLabNum.text = "0";
        numOperation=0
    }

    /// 等于
    @IBAction func juTouchResutl(_ sender: Any) {

        startOper = true
        let numSecond = (juLabNum.text! as NSString ).doubleValue
        var numSum = 0.0

        switch numOperation {
        case 1:
            numSum = numFirst+numSecond
            break
        case 2:
            numSum = numFirst-numSecond
            break
        case 3:
            numSum = numFirst*numSecond
            break
        case 4:
            numSum = numFirst/numSecond
            break
        default:
            numSum = numSecond
        }
        juLabNum.text=self.juDealWith(number: numSum)

         numFirst=(juLabNum.text! as NSString ).doubleValue
    }
    func juDealWith (number : Double) -> String {

        let numText = String(number)
        let arr = numText.components(separatedBy: ".")
        if arr.count>1{
            let pointTex : NSString = arr[1] as NSString
            if pointTex.intValue>0 {
                return String(number)
            }else{
                return arr[0]
            }
        }
        return arr[0]
    }
    /// 记录和清除
    @IBAction func juTouchMark(_ sender: UIButton) {
        if sender.tag == 1 {
            arrResult.add(juLabNum.text ?? "")
            juTxtResult.text = arrResult.componentsJoined(by: "  ")
        }else{
            arrResult.removeAllObjects()
            juTxtResult.text = ""
        }
        
    }
    @IBOutlet weak var juTouchOperation: UIButton!
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

